import { defineStore } from "pinia";
import shopcartApi, {
  type CartListData,
  type orderDetailData,
  type orderListData,
  type payInfoData,
  type tradeData,
} from "@/api/cartList";
interface shopCart {
  shopList: CartListData[];
  //   orderList: orderListData;
  skuId: number;
  orderId: number;
  orderList: tradeData;
  params: {
    phone: string;
    password: string;
  };
  token: string;
  payInfo: payInfoData
}

export const shopDataStore = defineStore("shopList", {
  state(): shopCart {
    return {
      shopList: [],
      skuId: 0,
      orderId: 22667,
      orderList: {
        detailArrayList: [],
        originalTotalAmount: 0,
        tradeNo: '',
        totalNum: 0,
        totalAmount: 0
      },
      params: {
        phone: "13994221196",
        password: "123456",
      },
      token: "",
      payInfo: {
        "codeUrl": '',
        "orderId": 0,
        "totalFee": 0,
        "resultCode": ''
      }
    };
  },
  actions: {
    async getShopList() {
      try {
        // console.log("===");

        const res = await shopcartApi.getCartList();

        // console.log(res);
        // console.log(res[0].cartInfoList);
        this.shopList = res[0].cartInfoList;
      } catch (error) {
        return Promise.reject(error);
      }
    },

    async getOrderList() {
      try {
        const res = await shopcartApi.reqTradeInfo();
        console.log(res);
        this.orderList = res
      } catch (error) {
        return Promise.reject(error);
      }
    },

    async CheckCartAll(isChecked: number, skuIdList: any) {
      try {
        await shopcartApi.reqBatchCheckCart(isChecked, skuIdList);
      } catch (error) {
        return Promise.reject(error);
      }
    },
    // async reqSubmitOrder() {
    //   try {
    //     const res = await shopcartApi.reqSubmitOrder(
    //       this.data.tradeNo,
    //       this.data
    //     );
    //     console.log(res);
    //     // this.orderId = res
    //   } catch (error) {
    //     return Promise.reject(error);
    //   }
    // },
    async reqPay(orderID: number) {
      try {
        const res = await shopcartApi.reqPay(orderID);
        // console.log(res);
        this.payInfo = res

      } catch (error) {
        return Promise.reject(error);
      }
    },
    async reqPayment(ordreId: number) {
      try {
        const res = await shopcartApi.reqPayment(ordreId);
        // console.log(res);
      } catch (error) {
        return Promise.reject(error);
      }
    },
    async getToken() {
      try {
        let res = await shopcartApi.getToken(this.params);
        // console.log(res);

        this.token = res.token;
        localStorage.setItem("token_key", this.token);
      } catch (error) {
        return Promise.reject(error);
      }
    },
  },
  getters: {},
});
